﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using XYX.Print.Client.PrintUI;
using XYX.Print.Impl;
using XYX.Print.Models.Models;
using XYX.Print.Utils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace XYX.Print.Client
{
    /// <summary>
    /// 主窗体
    /// </summary>
    public partial class MainForm : UIForm
    {
        /// <summary>
        /// 构造
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {

            UIStyles.InitColorful(Color.FromArgb(80, 160, 255), Color.Blue);
            uiTreeView.SelectedImageIndex = 0;
            SelectTreeView(uiTreeView, "ScanBarCode");
            this.checkIsRegister.Enabled = true;
            this.checkIsRegister.Interval = 600000 * 2;//2个小时检查一次
        }

        /// <summary>
        /// 打开窗体
        /// </summary>
        /// <param name="treeNode"></param>
        private void OpenSelectedForm(System.Windows.Forms.TreeNode treeNode)
        {

            if (treeNode.Tag.ToString() == "ScanBarCodeFormList")
            {
                OpenTabPage(treeNode, new ScanBarCodeFormList());
            }
            if (treeNode.Tag.ToString() == "ReportDesign")
            {
                OpenTabPage(treeNode, new ReportDesignForm());

            }
            if (treeNode.Tag.ToString() == "ScanBarCode")
            {
                OpenTabPage(treeNode, new ScanBarCodeForm());
            }
            if (treeNode.Tag.ToString() == "PrintRecode")
            {
                OpenTabPage(treeNode, new ScanBarCodePrintRecordList());
            }
        }

        private void OpenTabPage(System.Windows.Forms.TreeNode treeNode, Form form)
        {
            for (int i = 0; i < tabControl.Controls.Count; i++)
            {
                TabPage tp = tabControl.Controls[i] as TabPage;
                if (tp != null)
                {
                    if (tp.Text == treeNode.Text)
                    {
                        this.tabControl.SelectedIndex = i;
                        return;
                    }
                }
            }
            form.FormClosed += Form_FormClosed;
            form.MaximizeBox = false;
            form.MinimizeBox = false;
            form.TopLevel = false;
            form.FormBorderStyle = FormBorderStyle.None;

            System.Windows.Forms.Panel panelTab = new System.Windows.Forms.Panel();
            panelTab.Dock = DockStyle.Fill;
            panelTab.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
            form.BackColor = panelTab.BackColor;
            form.Dock = panelTab.Dock;

            TabPage tabPage = new TabPage();
            tabPage.Text = treeNode.Text;
            tabPage.ToolTipText = treeNode.ToolTipText;
            tabPage.Dock = DockStyle.Fill;
            this.tabControl.Controls.Add(tabPage);
            tabPage.Controls.Add(panelTab);
            tabPage.ImageIndex = 2;

            panelTab.Controls.Clear();
            panelTab.Controls.Add(form);
            this.tabControl.SelectedIndex = this.tabControl.TabCount - 1;
            form.Show();
        }

        /// <summary>
        /// 关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            var form = (Form)sender;
            if (form != null)
            {
                System.Windows.Forms.Panel panel = (System.Windows.Forms.Panel)form.Parent;
                if (panel != null)
                {
                    TabPage tabPage = (TabPage)panel.Parent;
                    tabControl.TabPages.Remove(tabPage);
                }
            }
        }
        /// <summary>
        /// 点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            Sunny.UI.UITreeView uITreeView = (Sunny.UI.UITreeView)sender;
            System.Windows.Forms.TreeNode treeNode = uITreeView.SelectedNode;
            OpenSelectedForm(treeNode);

        } /// <summary>
          /// 设置TreeView选中节点
          /// </summary>
          /// <param name="treeView"></param>
          /// <param name="tag">选中节点文本</param>
        private void SelectTreeView(Sunny.UI.UITreeView treeView, string tag)
        {
            treeView.Focus();
            for (int i = 0; i < treeView.Nodes.Count; i++)
            {
                for (int j = 0; j < treeView.Nodes[i].Nodes.Count; j++)
                {
                    if (treeView.Nodes[i].Nodes[j].Tag?.ToString() == tag)
                    {
                        treeView.SelectedNode = treeView.Nodes[i].Nodes[j];//选中
                                                                           //treeView.Nodes[i].Nodes[j].Checked = true;
                        treeView.Nodes[i].Expand();//展开父级
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            lbCurrentDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


        }

        /// <summary>
        /// 判断是否注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void checkIsRegister_Tick(object sender, EventArgs e)
        {
            SysRegistConfigImpl sysConfigImpl = new SysRegistConfigImpl();
            string cpu = Computer.GetComputerInfo().CPU;
            bool isRegisted = await sysConfigImpl.CheckIsRegisted(cpu);
            if (!isRegisted)
            {
                UIMessageBox.Show("未注册,请联系管理员");
                return;
            }
        }
    }
}
